/**
 * 权限资源管理
 */
layui.config({
  base: '/'
}).extend({
  treeTb: 'extend/treetable-lay/treetable'
}).define(['index', 'treeTb'], function (exports) {
  let treeTab = layui.treeTb;
  let admin = layui.admin;
  let layer = layui.layer;
  let dropdown = layui.dropdown;

  let expand_ids = [];

  let renderTable = treeTab.render({
    elem: '#LAY-resource-tree-table',	//表格id
    url: '/resource/getResourceTreeTableList',
    method: 'POST',
    height: 'full-160',
    tree: {
      iconIndex: 1,
      isPidData: true,
      idName: 'resId',
      pidName: 'parentId',
      openName: 'open',// 是否默认展开的字段名
    },
    //设置一个空的，占个位置，不要会显示默认的
    toolbar: '<div></div>',
    defaultToolbar: ['filter'],
    page: false,
    cols: [[
      {type: 'numbers', fixed: 'left', title: '序号'},
      {field: 'resId', title: '资源编码', minWidth: 130},
      {field: 'resName', title: '资源名称', minWidth: 130},
      {
        field: 'type', title: '类型', templet: function (d) {
          if (d.type == 'S') {
            return '<span class="layui-bg-green layui-badge-rim">模块</span>';
          } else if (d.type == 'M') {
            return '<span class="layui-bg-blue layui-badge-rim">菜单</span>';
          } else {
            return '<span class="layui-bg-orange layui-badge-rim">按钮</span>';
          }
        }, minWidth: 63, unresize: true
      },
      {field: 'title', title: '标题', minWidth: 100},
      {field: 'href', title: '链接地址', minWidth: 200},
      {field: 'mid', title: '权限标识', minWidth: 100},
      {field: 'icon', title: '图标', minWidth: 100},
      {field: 'seq', title: '排序', minWidth: 60},
      {field: 'status', title: '状态', templet: '#status-switchTpl', unresize: true, minWidth: 90},
      {field: 'updatedBy', title: '修改人', minWidth: 90},
      {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#resource-tree-table'}
    ]],
    //数据渲染完的回调
    done: function (data) {
      //这里默认展开两级
      for (let i = 0; i < data.length; i++) {
        expand_ids.push(data[i].resId);
        let children = data[i].children;
        if (children && children.length > 0) {
          for (let a = 0; a < children.length; a++) {
            expand_ids.push(children[a].resId);
          }
        }
      }
      //关闭加载
      layer.closeAll('loading');

      for (let i = 0; i < expand_ids.length; i++) {
        renderTable.expand(expand_ids[i]);  // 展开指定节点
      }
    },
    style: 'margin-top:0;'
  });

  //监听工具条
  treeTab.on('tool(LAY-resource-tree-table)', function (obj) {
    let line_data = obj.data;
    if (obj.event === 'add') {
      layer.open({
        type: 2
        , title: '新增'
        , content: '/resource/turn/edit?resId=' + line_data.resId
        // ,maxmin: true
        , area: ['98.3%', '98.3%']
        , resize: false
        , yes: function (index, layero) {
        }
        , cancel: function (index, layero) {
          layer.close(index);
          renderTable.refresh(); //数据刷新
          return false;
        }
        //弹层被关闭且销毁后的回调函数
        , end: function () {
          renderTable.refresh(); //数据刷新
        }
      });
    } else if (obj.event === 'more') {
      //下拉菜单
      dropdown.render({
        elem: this, // 触发事件的 DOM 对象
        show: true, // 外部事件触发即显示
        align: 'left ' , // 右对齐弹出
        data: [
          {
            title: '修改',
            id: 'edit',
            templet: '<i class="layui-icon layui-icon-edit"></i> {{= d.title}}',
            href: '#'
          },
          {
            title: '删除',
            id: 'del',
            templet: '<i class="layui-icon layui-icon-delete"></i> {{= d.title }}',
            href: '#'
          }
        ],
        click: function (mdata) {
          if (mdata.id === "edit") {
            if ('root' == line_data.resName) {
              layer.msg('根节点不允许修改！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
              return;
            }
            layer.open({
              type: 2
              , title: '修改'
              , content: '/resource/turn/edit?id=' + line_data.id
              // ,maxmin: true
              , area: ['98.3%', '98.3%']
              , resize: false
              , yes: function (index, layero) {
              }
              , cancel: function (index, layero) {
                layer.close(index);
                renderTable.refresh(); //数据刷新
                return false;
              }
              //弹层被关闭且销毁后的回调函数
              , end: function () {
                renderTable.refresh(); //数据刷新
              }
            });
          } else if (mdata.id === "del") {
            if ('root' == line_data.resName) {
              layer.msg('根节点不允许删除！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
              return;
            }
            if (line_data.children && line_data.children.length > 0) {
              layer.msg('当前资源节点存在下级资源不允许删除！', {icon: 7, time: 1000, shade: [0.3, '#000', true]});
              return;
            }
            layer.confirm('真的删除行么？', function (index) {
              //提交 Ajax 成功后，更新表格中的数据
              admin.req({
                url: '/resource/del'
                , type: 'post'
                , contentType: 'application/json;charset=utf-8'
                , data: JSON.stringify({"id": line_data.id})
                , done: function (res) {
                  //执行成功
                  layer.msg(res.msg, {icon: 1});
                  renderTable.refresh(); //数据刷新
                  layer.close(index); //关闭弹层
                }
              });
            });
          }
        }
      });
    }
  });

  layui.$('#btn-expand').click(function () {
    renderTable.expandAll('#LAY-resource-tree-table');
  });

  layui.$('#btn-fold').click(function () {
    renderTable.foldAll('#LAY-resource-tree-table');
  });

  layui.$('#btn-refresh').click(function () {
    renderTable.refresh();
  });

  //对外暴露的接口
  exports('resource', {});
});
